import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

public class Card extends JLabel {
	public static String defaultIcon = "defaultcard";
	public static String backside = "backside";
	public static String empty = "empty";
	private Card thisCard = this;
	private boolean clickable;
	private String name;

	public Card(String name) {
		Icon icon = new ImageIcon("rsc/" + name + ".png"); // TODO:
		setIcon(icon);
		setName(name);
		clickable = false;
		this.name = name; 
		addMouseListener(new PopClickListener());
	}
//	
//	public String toString() {
//		return name;
//	}
//	
	
	/**
	 * returns the name of the card(describing the picture)
	 */
	public String getName(){
		return name;
	}
	
	public void setClickable(boolean clickable) {
		this.clickable = clickable;
	}
	
	/**
	 * switches the mode of the card between clickable and not clickable.
	 */
	public void switchClickable() {
		this.clickable = !this.clickable;
	}

	class PopClickListener implements MouseListener {
		@Override
		public void mouseClicked(MouseEvent e) {
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (clickable && e.isPopupTrigger())
				doPop(e);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (clickable && e.isPopupTrigger())
				doPop(e);
		}

		private void doPop(MouseEvent e) {
			PopUpMenu menu = new PopUpMenu();
			menu.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	class PopUpMenu extends JPopupMenu {
		JMenuItem putTable, putStack;

		public PopUpMenu() {
			putTable = new JMenuItem("Put to Table");
			putTable.addActionListener(new CardActionListener());
			putStack = new JMenuItem("Put to Stack");
			putStack.addActionListener(new CardActionListener());
			add(putTable);
			add(putStack);
		}
	}
	
	/**
	 * if this card is hold by the player, it returns the index of this card. returns handsize +1 if he does not hold this card
	 * @return the index of the card in the holding hand
	 */
	private int getYourIndex(){
		Hand ownH = Game.getGame().client.getOwnHand();
		int cardCount = ownH.getComponentCount();
		int i;
		for(i = 0; i < cardCount; ++i){
			if(ownH.getComponent(i).equals(thisCard)){
				break;
			}
		}
		return i;
	}

	class CardActionListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			if (((JMenuItem) e.getSource()).getText().equals("Put to Table")) {
				Game.getGame().client.sendCardToTable(thisCard.getName(),thisCard.getName(),getYourIndex());
			} else if (((JMenuItem) e.getSource()).getText().equals("Put to Stack")) {
				Game.getGame().client.sendCardToTable(thisCard.getName(),JOptionPane.showInputDialog("Name of the Stack"),getYourIndex());
			}
		}

	}
}
